package com.toutou.base.util;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.toutou.base.R;


/**
 * 下一个Toast覆盖上一个Toast,防止Toast弹出的时间过长
 */
public class ToastUtil {

    private static Toast toast;

    /**
     * 在屏幕正中间弹短吐司
     */
    public static void showToast(String message) {
        Context context = ActivitiesManager.INSTANCE.topActivity();
        if (context == null) {
            return;
        }
        View view= LayoutInflater.from(context).inflate(R.layout.item_toast,null);
        TextView tv_msg = (TextView) view.findViewById(R.id.toast_txt);
        tv_msg.setText(message);
        if (toast != null) {
            toast.cancel();
            toast = null;
        }
        toast = Toast.makeText(context, message, Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.setView(view);
        tv_msg.setGravity(Gravity.CENTER);
        toast.show();
    }
}
